{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1c38b404",
   "metadata": {},
   "source": [
    "# week12 回顾\n",
    "* json（文本数据）---> dict（python中的字典数据）\n",
    "* 字典和列表的综合实践"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3912688f",
   "metadata": {},
   "source": [
    "# 本周内容：\n",
    "* 集合\n",
    "（思考：我想了解一组数据中有哪些唯一存在的值？假如是列表）\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e52a26ef",
   "metadata": {},
   "source": [
    "### 集合的性质\n",
    "> 1. 无序\n",
    "> 2. 不重复\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2341651e",
   "metadata": {},
   "outputs": [],
   "source": [
    "no_duplicate_age = []\n",
    "list_age = [18,24,23,23,256,23,18]\n",
    "for i in list_age:\n",
    "    if i not in no_duplicate_age:\n",
    "        no_duplicate_age.append(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "eb25273d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 24, 23, 256]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "no_duplicate_age"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ef821e8e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{24, 256, 18, 23}\n"
     ]
    }
   ],
   "source": [
    "# list -----> set 的方法：set()\n",
    "no_duplicate_age_set = set(list_age)\n",
    "print(no_duplicate_age_set)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "16717657",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'banana', 'apple', 'pear', 'orange'}\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# 官方文档测试代码\n",
    "#1. 移除重复项\n",
    "basket = {'apple','orange','apple','pear','orange','banana'}\n",
    "print(basket)\n",
    "# 2. in查询成员关系\n",
    "print('orange'in basket)\n",
    "print('crabgrass' in basket)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ca33981d",
   "metadata": {},
   "source": [
    "### 集合的一般方法\n",
    "> 1. 增加\n",
    "> 2.删除\n",
    "> 3.查询：in查询成员关系\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1fdb54a8",
   "metadata": {},
   "source": [
    "* 集合的创建的两种一般方法：\n",
    "> 1.set(list)\n",
    "> 2.直接按照集合的结构创建：{value1，value2，value3，……}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "5e96897e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 66, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "# 增加\n",
    "set_1 = set([1,3,4,0,8,66,7])\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "348071c2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function add:\n",
      "\n",
      "add(...) method of builtins.set instance\n",
      "    Add an element to a set.\n",
      "    \n",
      "    This has no effect if the element is already present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set_1.add)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0860dc8d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 删除\n",
    "> \n",
    ">"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7a655fbf",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(set_1)\n",
    "set_1.pop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "40c9e29a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# * 避免keyerror的方式\n",
    "if 66 in set_1:\n",
    "    set_1.remove(66)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c739f1ab",
   "metadata": {},
   "outputs": [],
   "source": [
    "help(set_1.discard)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "51a757d5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c1c9892f",
   "metadata": {},
   "outputs": [],
   "source": [
    "help(set_1.remove)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1252dad1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.remove an element from a set:it must be \n",
    "print(set_1)\n",
    "set_1.remove(66)\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "96b3679b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2.if the element is not a member, raise a keyerror .一般项目中remove()会和in 共同使用\n",
    "set_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e5cdc6c7",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "a6c12adb",
   "metadata": {},
   "source": [
    "### 集合的数学逻辑运算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc8e7765",
   "metadata": {},
   "source": [
    "> 1.集合的交集\n",
    ">> 1. 使用集合的方法：set_1.intersection(set_2)\n",
    ">> 2. set_1 & set_2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "0fb6d3d6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1: {1, 2, 3, 4, 5, 6, 7, 8}\n",
      "set_2: {1, 2, 3, 4, 5, 6, 7, 88}\n"
     ]
    }
   ],
   "source": [
    "set_1 =set([1,2,5,6,7,8,2,3,4,4,4])\n",
    "set_2 =set([1,3,4,5,7,88,6,5,2,5])\n",
    "print(\"set_1:\",set_1)\n",
    "print(\"set_2:\",set_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "0f6c2ab1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function intersection:\n",
      "\n",
      "intersection(...) method of builtins.set instance\n",
      "    Return the intersection of two sets as a new set.\n",
      "    \n",
      "    (i.e. all elements that are in both sets.)\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set_1.intersection)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "eacfe742",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1: {1, 2, 3, 4, 5, 6, 7, 8}\n",
      "set_2: {1, 2, 3, 4, 5, 6, 7, 88}\n",
      "交集结果： {1, 2, 3, 4, 5, 6, 7}\n"
     ]
    }
   ],
   "source": [
    "print(\"set_1:\",set_1)\n",
    "print(\"set_2:\",set_2)\n",
    "print(\"交集结果：\",set_1.intersection(set_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "c539ddea",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1: {1, 2, 3, 4, 5, 6, 7, 8}\n",
      "set_2: {1, 2, 3, 4, 5, 6, 7, 88}\n",
      "交集结果： {1, 2, 3, 4, 5, 6, 7}\n"
     ]
    }
   ],
   "source": [
    "print(\"set_1:\",set_1)\n",
    "print(\"set_2:\",set_2)\n",
    "print(\"交集结果：\", set_1 & set_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "49f6d846",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "28298bb0",
   "metadata": {},
   "source": [
    "> 2. 集合的并集\n",
    ">> 1.使用并集的方法： set_1.union(set_2)  \n",
    ">> 2.使用逻辑运算符 set_1 | set_2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "10be3f1f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1: {1, 2, 3, 4, 5, 6, 7, 8}\n",
      "set_2: {1, 2, 3, 4, 5, 6, 7, 88}\n",
      "并集结果： {1, 2, 3, 4, 5, 6, 7, 8, 88}\n"
     ]
    }
   ],
   "source": [
    "#使用并集的方法： set_1.union(set_2)\n",
    "print(\"set_1:\",set_1)\n",
    "print(\"set_2:\",set_2)\n",
    "print(\"并集结果：\",set_1.union(set_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "cccfc304",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1: {1, 2, 3, 4, 5, 6, 7, 8}\n",
      "set_2: {1, 2, 3, 4, 5, 6, 7, 88}\n",
      "并集结果： {1, 2, 3, 4, 5, 6, 7, 8, 88}\n"
     ]
    }
   ],
   "source": [
    "# 使用逻辑运算符 set_1 | set_2\n",
    "print(\"set_1:\",set_1)\n",
    "print(\"set_2:\",set_2)\n",
    "print(\"并集结果：\",set_1 | set_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "21b2fac9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "开始的时间： 1637810973.3171601\n",
      "set_1: {1, 2, 3, 4, 5, 6, 7, 8}\n",
      "set_2: {1, 2, 3, 4, 5, 6, 7, 88}\n",
      "并集结果： {1, 2, 3, 4, 5, 6, 7, 8, 88}\n",
      "结束的时间： 1637810973.3191206\n",
      "运行的时间： 0.0029578208923339844\n"
     ]
    }
   ],
   "source": [
    "# 计算运行效率 运行后的时间节点 - 运行前的时间节点 :思考并对比列表运算的执行效率\n",
    "import time\n",
    "start_time = time.time()\n",
    "print(\"开始的时间：\",time.time())\n",
    "print(\"set_1:\",set_1)\n",
    "print(\"set_2:\",set_2)\n",
    "print(\"并集结果：\",set_1 | set_2)\n",
    "stop_time = time.time()\n",
    "print(\"结束的时间：\", stop_time)\n",
    "print(\"运行的时间：\", stop_time - start_time)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa18373a",
   "metadata": {},
   "source": [
    "> 3. 集合的差集(考虑先后顺序)\n",
    ">> 1.使用差集的方法：set_1.difference(set_2)  \n",
    ">> 2.使用逻辑运算符：set_1 - set_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "1e14ab2a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1: {1, 2, 3, 4, 5, 6, 7, 8}\n",
      "set_2: {1, 2, 3, 4, 5, 6, 7, 88}\n",
      "差集结果： {8}\n"
     ]
    }
   ],
   "source": [
    "print(\"set_1:\",set_1)\n",
    "print(\"set_2:\",set_2)\n",
    "print(\"差集结果：\",set_1.difference(set_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "8679be12",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1: {1, 2, 3, 4, 5, 6, 7, 8}\n",
      "set_2: {1, 2, 3, 4, 5, 6, 7, 88}\n",
      "差集结果： {8}\n"
     ]
    }
   ],
   "source": [
    "print(\"set_1:\",set_1)\n",
    "print(\"set_2:\",set_2)\n",
    "print(\"差集结果：\",set_1 - set_2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ab8b0b89",
   "metadata": {},
   "source": [
    "> 4. 对称差集\n",
    ">> 1.用对称差集的方法：set_1.symmetric_difference(set_2)\n",
    ">> 2. 使用逻辑运算符：？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "6052d212",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1: {1, 2, 3, 4, 5, 6, 7, 8}\n",
      "set_2: {1, 2, 3, 4, 5, 6, 7, 88}\n",
      "对称差集结果： {8, 88}\n"
     ]
    }
   ],
   "source": [
    "print(\"set_1:\",set_1)\n",
    "print(\"set_2:\",set_2)\n",
    "print(\"对称差集结果：\",set_1.symmetric_difference(set_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5db3ce05",
   "metadata": {},
   "outputs": [],
   "source": [
    "集合实践：（p129）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7b394028",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
